//
//  ZHLEEStatus.swift
//  ZHLEE_Blog_Swift_Pro
//
//  Created by ZHLEEBlander on 15/10/15.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEEStatus: NSObject {

    var id: Int = 0
    
    //微博内容
    var text: String?
    
    //user当前微博的作者
    var user: ZHLEEUser?
    /// 创建时间
    var created_at: String?
        
    var createdAtStr: String {
        
            var result = ""
            
             //    Sat Oct 17 11:35:32 +0800 2015
            let formatter = NSDateFormatter()
            
            formatter.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
            
            formatter.locale = NSLocale(localeIdentifier: "en_US")
            
            //测试数据
//            created_at = "2015-10-21 20:30:00"
//            formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            
            let createDate = formatter.dateFromString(created_at!)
        
            let currentDate = NSDate()
            
            if currentDate.isThisYear(createDate!) {
            
//                printLog("今年")
                
                if currentDate.isToday(createDate!){
                
                    result = "是今天"
                    
                    let currentDate = NSDate()
                    
                    //现在对比发微博的时间///秒数
                    let timeInterval = currentDate.timeIntervalSinceDate(createDate!)
                    
                    
                    if timeInterval < 60 {//一分钟内
                    
                        result = "刚刚"
                    }else if timeInterval < 60 * 60 {//一个小时内
                    
                        result = "\(Int(timeInterval) / 60)分钟前"
                    }else{//一个小时后
                    
                        result = "\(Int(timeInterval) / (60 * 60))小时前"
                    }
                    
                    
                }else if currentDate.isYesToday(createDate!){
                
                    formatter.dateFormat = "昨天 HH:mm"
                    result = formatter.stringFromDate(createDate!)
                }else{
                
                    formatter.dateFormat = "MM-dd HH:mm"
                    result = formatter.stringFromDate(createDate!)
                }
            }else{
            
                formatter.dateFormat = "yyyy-MM-dd"
                
                result = formatter.stringFromDate(createDate!)
            }
            
           return result
    }
    /// 来源
    var source: String?{
    
        didSet{
            //<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>
            
            //字符串处理
            let sourceStr = (source! as NSString)
            
            let preRange = sourceStr.rangeOfString("\">")
            
            if preRange.location != NSNotFound{
            
                let suRange = sourceStr.rangeOfString("</")
                
//              开始截取的位置
                let location = preRange.location + preRange.length
//                截取的长度
                let length = suRange.location - location
                
                source = "来自\(sourceStr.substringWithRange(NSMakeRange(location, length)))"
                
            }
            
        }
    }
    
    //图片列表
    var pic_urls: [ZHLEEStatusPhotoInfo]?
    
    //转发数
    var reposts_count: Int = 0
    //评论数
    var comments_count: Int = 0
    //表态数
    var attitudes_count: Int = 0
    
    //转发微博
    var retweeted_status: ZHLEEStatus?
    
    // MARK: -
    // MARK: -----------------方法--------------------------
    
    init(dictionary: [String: AnyObject]){
    
        super.init()
        
        setValuesForKeysWithDictionary(dictionary)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if (key as NSString).isEqualToString("user"){
        
            user = ZHLEEUser(dictionary: (value as! [String: AnyObject]))
        }else if(key as NSString).isEqualToString("retweeted_status"){
            retweeted_status = ZHLEEStatus.init(dictionary: (value as! [String : AnyObject]))
        }else if(key as NSString).isEqualToString("pic_urls"){
        
            pic_urls = [ZHLEEStatusPhotoInfo]()
            
            if let values = (value as? [[String: AnyObject]]){
            
                for tempV in values {
                    let photoInfo = ZHLEEStatusPhotoInfo(dict: tempV)
                    pic_urls?.append(photoInfo)
                }
            }
        }
        else{
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
}
